package com.learn._4Q;


import com.learn.TestTool;
import com.learn._3stack.LinkedStack;
import com.learn._3stack.SimpleStack;
import com.learn._3stack.Stack;

import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * 循环队列，最大能存储数量arraylength - 1
 * @param <T>
 */
public class SimpleQueue<T> implements Queue<T> {

    private Object[] array;
    private int front = -1;
    private int rear = front;


    public SimpleQueue(int size){
       array = new Object[size];
    }

    @Override
    public void offer(T obj) {
        int tmp;
        if(rear == array.length - 1){
            tmp = 0;
        }else{
            tmp = rear + 1;
        }
        if(tmp == front){
            throw new RuntimeException("queue full");
        }
        rear = tmp;
        array[rear] = obj;
    }

    @Override
    public T poll() {
        T res = peek();
        //goNextFront();
        return res;
    }

    private void goNextFront() {
        if(front < array.length - 1){
            front ++;
        }else{
            front  = 0;
        }
    }

    @Override
    public T peek() {

        if(front == rear){
            throw new RuntimeException("queue empty");
        }
        goNextFront();
        return (T) array[front];
    }




    public static void main(String[] args) {

        java.util.Queue arrayQueue = new ArrayDeque();
        java.util.Queue linkedQueueJava = new LinkedList();
        LinkedQueue linkedQueue = new LinkedQueue();

        int num = 10000000;
        java.util.Queue arrayQueue1 = new ArrayBlockingQueue(num);
        SimpleQueue myQueue= new SimpleQueue(num + 1);
        int range = 1000000;
        int circleNum = 100;
        Integer[] array = new Integer[num];
        Random rand = new Random();
        int tmp = 0;
        int tmpNum = num;
        while (tmpNum-- > 0) {
            array[tmp++] = rand.nextInt(range);
        }

      /*  for(Integer i :array){
            javaList.add(i);
            arrayList.add(i);
        */



        //删测试

       /*TestTool.testAndPrintTime(()->

        {
            for (int i = 0; i < num; i++) {
                javaStack.push(i);
            }

            for (int i = 0; i < num; i++) {
                //System.out.println(javaStack.pop());
                javaStack.pop();
            }
            //javaList.clear();
        },1,"java ");*/

    /*   TestTool.testAndPrintTime(()->

        {
        for (int i = 0; i < num; i++) {
            myStack.push(i);
        }

        for (int i = 0; i < num; i++) {
            //System.out.println(myStack.pop());
            myStack.pop();
        }
        //arrayList.clear();
    },1,"my  ");*/

        TestTool.testAndPrintTime(()->

        {
            for (int i = 0; i < num; i++) {
                myQueue.offer(i);
            }

            for (int i = 0; i < num; i++) {
                //System.out.println(myStack.pop());
                myQueue.poll();
            }
            //arrayList.clear();
        },1,"my  ");









    }
}
